<?php
namespace app\common\model;

use think\Model;

/**
 * 归因安装模型
 * 存储安装归因结果
 */
class AttributionInstall extends Model
{
    /**
     * 数据表名
     */
    protected $table = 'gf_attribution_install';
    
    /**
     * 主键
     */
    protected $pk = 'id';
    
    /**
     * 自动时间戳
     */
    protected $autoWriteTimestamp = true;
    
    /**
     * 创建时间字段
     */
    protected $createTime = 'attributed_at';
    
    /**
     * 更新时间字段
     */
    protected $updateTime = false;
    
    /**
     * 字段信息
     */
    protected $schema = [
        'id' => 'int',
        'install_id' => 'string',
        'appkey' => 'string',
        'device_id' => 'string',
        'fingerprint' => 'string',
        'tracking_id' => 'string',
        'channel' => 'string',
        'match_type' => 'string',
        'confidence' => 'float',
        'install_time' => 'datetime',
        'attributed_at' => 'datetime',
        'custom_params' => 'json'
    ];
    
    /**
     * 获取安装记录
     */
    public function getByInstallId($installId)
    {
        return $this->where('install_id', $installId)->find();
    }
    
    /**
     * 获取设备的安装记录
     */
    public function getByFingerprint($appkey, $fingerprint)
    {
        return $this->where([
            'appkey' => $appkey,
            'fingerprint' => $fingerprint
        ])->order('install_time', 'desc')->select();
    }
    
    /**
     * 获取应用的安装记录
     */
    public function getByAppkey($appkey, $startDate = null, $endDate = null)
    {
        $query = $this->where('appkey', $appkey);
        
        if ($startDate) {
            $query->where('install_time', '>=', $startDate);
        }
        
        if ($endDate) {
            $query->where('install_time', '<=', $endDate);
        }
        
        return $query->order('install_time', 'desc')->select();
    }
    
    /**
     * 获取渠道的安装记录
     */
    public function getByChannel($appkey, $channel, $startDate = null, $endDate = null)
    {
        $query = $this->where([
            'appkey' => $appkey,
            'channel' => $channel
        ]);
        
        if ($startDate) {
            $query->where('install_time', '>=', $startDate);
        }
        
        if ($endDate) {
            $query->where('install_time', '<=', $endDate);
        }
        
        return $query->order('install_time', 'desc')->select();
    }
    
    /**
     * 获取匹配类型统计
     */
    public function getMatchTypeStats($appkey, $startDate = null, $endDate = null)
    {
        $query = $this->where('appkey', $appkey)
                    ->field('match_type, COUNT(*) as count, AVG(confidence) as avg_confidence')
                    ->group('match_type');
        
        if ($startDate) {
            $query->where('install_time', '>=', $startDate);
        }
        
        if ($endDate) {
            $query->where('install_time', '<=', $endDate);
        }
        
        return $query->select();
    }
    
    /**
     * 获取渠道统计
     */
    public function getChannelStats($appkey, $startDate = null, $endDate = null)
    {
        $query = $this->where('appkey', $appkey)
                    ->field('channel, COUNT(*) as installs, AVG(confidence) as avg_confidence')
                    ->group('channel')
                    ->order('installs', 'desc');
        
        if ($startDate) {
            $query->where('install_time', '>=', $startDate);
        }
        
        if ($endDate) {
            $query->where('install_time', '<=', $endDate);
        }
        
        return $query->select();
    }
    
    /**
     * 获取安装趋势
     */
    public function getInstallTrend($appkey, $startDate, $endDate)
    {
        return $this->where('appkey', $appkey)
                   ->whereBetween('install_time', [$startDate, $endDate])
                   ->field('DATE(install_time) as date, COUNT(*) as installs, COUNT(DISTINCT channel) as channels')
                   ->group('DATE(install_time)')
                   ->order('date', 'asc')
                   ->select();
    }
}